package com.uenpay.im.ui.old.activity.label;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-4-22 上午11:37:10
 * @copyRight http://www.qiyunxin.com
 * @function 标签管理
 */
public class LabelHandle {

	public interface ILabelResultListener {
		void onLabelResult(int status, String msg);
	}

	public interface IGetMyLabelsListener {
		void onGetMyLabelsResult(int status, String msg,
										ArrayList<LabelEntity> arrayList);
	}

	/**
	 * 获取我的标签列表
	 * 
	 * @param mIGetMyLabelsListener
	 */
	public void getMyLabels(final IGetMyLabelsListener mIGetMyLabelsListener) {
		GetLabelsInvokeItem item = new GetLabelsInvokeItem();
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {
					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {

							GetLabelsInvokeItem.GetLabelsInvokeItemResult result = ((GetLabelsInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mIGetMyLabelsListener.onGetMyLabelsResult(
										result.status, result.msg,
										result.arrayList);
							} else {
								mIGetMyLabelsListener.onGetMyLabelsResult(-1,
										"", null);
							}
						} else {
							mIGetMyLabelsListener.onGetMyLabelsResult(-1,
									failReason, null);
						}
					}
				});
	}

	/**
	 * 删除标签
	 * 
	 * @param label_id
	 * @param mILabelResultListener
	 */
	public void deleteLabel(int label_id,
			final ILabelResultListener mILabelResultListener) {
		DeleteLabelInvokeItem item = new DeleteLabelInvokeItem(label_id);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {
					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							DeleteLabelInvokeItem.DeleteLabelInvokeItemResult result = ((DeleteLabelInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mILabelResultListener.onLabelResult(
										result.status, result.msg);
							} else {
								mILabelResultListener.onLabelResult(-1,
										failReason);
							}
						} else {
							mILabelResultListener.onLabelResult(-1, failReason);
						}
					}
				});
	}

	/**
	 * 创建标签
	 * 
	 * @param tagName
	 * @param custIds
	 */
	public void createLabel(String tagName, String custIds,
			final ILabelResultListener mLabelResultListener) {
		CreateLabelInvokeItem item = new CreateLabelInvokeItem(tagName, custIds);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {
					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							CreateLabelInvokeItem.CreateLabelInvokeItemResult result = ((CreateLabelInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mLabelResultListener.onLabelResult(
										result.status, result.msg);
							} else {
								mLabelResultListener.onLabelResult(-1, "");
							}
						} else {
							mLabelResultListener.onLabelResult(-1, failReason);
						}
					}
				});
	}

	/**
	 * 修改标签
	 * 
	 * @param label_id
	 * @param name
	 * @param custids
	 * @param mLabelResultListener
	 */
	public void updateLabel(int label_id, String name, String custids,
			final ILabelResultListener mLabelResultListener) {
		UpdateLabelInvokeItem item = new UpdateLabelInvokeItem(label_id, name,
				custids);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {
					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							UpdateLabelInvokeItem.UpdateLabelInvokeItemResult result = ((UpdateLabelInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mLabelResultListener.onLabelResult(
										result.status, result.msg);
							} else {
								mLabelResultListener.onLabelResult(-1, "");
							}
						} else {
							mLabelResultListener.onLabelResult(-1, failReason);
						}
					}
				});

	}
}
